import enum

from sqlalchemy import Column, Integer, String, DateTime, Numeric, Enum
from sqlalchemy.orm import relationship
from sqlalchemy.sql import func

from app.config import Base


class FlightStatus(str, enum.Enum):
    scheduled = "scheduled"
    delayed = "delayed"
    cancelled = "cancelled"
    departed = "departed"
    arrived = "arrived"


class Flight(Base):
    __tablename__ = "flights"

    id = Column(Integer, primary_key=True, index=True)
    flight_number = Column(String(10), unique=True, index=True)
    airline = Column(String(50))
    departure_city = Column(String(50))
    arrival_city = Column(String(50))
    departure_time = Column(DateTime(timezone=True))
    arrival_time = Column(DateTime(timezone=True))
    departure_airport = Column(String(50))
    arrival_airport = Column(String(50))
    price = Column(Numeric(10, 2))
    total_seats = Column(Integer)
    available_seats = Column(Integer)
    status = Column(Enum(FlightStatus, name='flight_status'), default=FlightStatus.scheduled)
    created_at = Column(DateTime(timezone=True), server_default=func.now())
    updated_at = Column(DateTime(timezone=True), onupdate=func.now())

    bookings = relationship("Booking", back_populates="flight")

    def __repr__(self):
        return f"<Flight {self.flight_number}>"
